﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYCoordinate.Properties;
using YJKRGeometry;

namespace HYCoordinate
{
	public partial class FormUse : System.Windows.Forms.Form
	{
		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		internal bool GetIfDrawSlope()
		{
			return this.ifDrawSlopeTag;
		}

		public void LockControles()
		{
			if (!this.FreeControlTag)
			{
				this.checkBox_IfSlope.Enabled = false;
			}
		}

		public void SetComboBoxValue(double input)
		{
			if (0.0 >= input || this.comboBoxNeedValue == null)
			{
				return;
			}
			this.comboBoxNeedValue.Text = input.ToString("F2");
			this.comboBoxNeedValue = null;
		}

		private void comboxesValueReSet()
		{
			double num = 100.0;
			foreach (ComboBox comboBox in this.inputCombx)
			{
				num = 100.0;
				if (!double.TryParse(comboBox.Text, out num))
				{
					if (comboBox.Items.Count > 0)
					{
						comboBox.Text = comboBox.Items[0].ToString();
					}
				}
				else
				{
					comboBox.Text = num.ToString("F2");
				}
				FormUse.AddComboxItem(comboBox);
			}
		}

		private void makeControlList()
		{
			this.inputCombx.Clear();
			this.selectControl.Clear();
			this.selectControl.Add(this.comboBox_DitchKind);
			this.selectControl.Add(this.comboBox_CornerKind);
			this.selectControl.Add(this.checkBox_IfSlope);
			this.inputCombx.Add(this.comboBox_Radius);
			this.inputCombx.Add(this.comboBox_Distance);
			this.inputCombx.Add(this.comboBox_FlateWidth);
			this.inputCombx.Add(this.comboBox_Deep);
			this.inputCombx.Add(this.comboBox_Thickness);
			this.inputCombx.Add(this.comboBox_Slope);
		}

		private XmlElement findXmlElement()
		{
			XmlElement result = null;
			string text = StaticRescource.ParamXmlPath + StaticRescource.ParamXmlName;
			if (!File.Exists(text))
			{
				this.creatXmlDocument();
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			foreach (object obj in xmlDocument.DocumentElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode is XmlElement)
				{
					XmlElement xmlElement = xmlNode as XmlElement;
					if (this.m_nMajorIndex == int.Parse(xmlElement.GetAttribute(StaticRescource.ParamArrtibuteNames[0])))
					{
						result = xmlElement;
					}
				}
			}
			return result;
		}

		private void getXmlValue()
		{
			XmlElement xmlElement = this.findXmlElement();
			if (xmlElement == null)
			{
				return;
			}
			int num = 0;
			foreach (object obj in xmlElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode is XmlElement)
				{
					if (num == 0)
					{
						for (int i = 0; i < (xmlNode as XmlElement).Attributes.Count; i++)
						{
							Control control = this.selectControl[i];
							if (control is ComboBox)
							{
								(control as ComboBox).SelectedIndex = int.Parse((xmlNode as XmlElement).Attributes[i].Value);
							}
							else if (control is CheckBox)
							{
								if (int.Parse((xmlNode as XmlElement).Attributes[i].Value) == 0)
								{
									(control as CheckBox).Checked = false;
								}
								else
								{
									(control as CheckBox).Checked = true;
								}
							}
						}
						num++;
					}
					else if (num == 1)
					{
						for (int j = 0; j < (xmlNode as XmlElement).Attributes.Count; j++)
						{
							ComboBox comboBox = this.inputCombx[j];
							comboBox.Text = (xmlNode as XmlElement).Attributes[j].Value;
							FormUse.AddComboxItem(comboBox);
						}
						num++;
					}
				}
			}
		}

		private void setXmlValue()
		{
			XmlElement xmlElement = this.findXmlElement();
			if (xmlElement == null)
			{
				return;
			}
			int num = 0;
			foreach (object obj in xmlElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode is XmlElement)
				{
					if (num == 0)
					{
						for (int i = 0; i < (xmlNode as XmlElement).Attributes.Count; i++)
						{
							Control control = this.selectControl[i];
							if (control is ComboBox)
							{
								(xmlNode as XmlElement).Attributes[i].Value = (control as ComboBox).SelectedIndex.ToString();
							}
							else if (control is CheckBox)
							{
								if (!(control as CheckBox).Checked)
								{
									(xmlNode as XmlElement).Attributes[i].Value = 0.ToString();
								}
								else
								{
									(xmlNode as XmlElement).Attributes[i].Value = 1.ToString();
								}
							}
						}
						num++;
					}
					else if (num == 1)
					{
						for (int j = 0; j < (xmlNode as XmlElement).Attributes.Count; j++)
						{
							ComboBox comboBox = this.inputCombx[j];
							(xmlNode as XmlElement).Attributes[j].Value = comboBox.Text;
						}
						num++;
					}
				}
			}
			string filename = StaticRescource.ParamXmlPath + StaticRescource.ParamXmlName;
			xmlElement.OwnerDocument.Save(filename);
		}

		private void sendEscapeMessage(bool ifChangerControlTag = true)
		{
			int wMsg = 7;
			FormUse.SendMessage(this.m_mainWindowHandle, wMsg, IntPtr.Zero, IntPtr.Zero);
			if (ifChangerControlTag)
			{
				DrainMakerApplication.UseMessageTag = false;
			}
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormUse.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormUse.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void creatXmlDocument()
		{
			string filename = StaticRescource.ParamXmlPath + StaticRescource.ParamXmlName;
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement = xmlDocument.CreateElement("DitchValue");
			XmlNode xmlNode = xmlElement;
			XmlDocument doc = xmlDocument;
			int id = 0;
			int[] array = new int[3];
			array[0] = 3;
			xmlNode.AppendChild(this.creatXmlElement(doc, id, array, new double[]
			{
				300.0,
				300.0,
				400.0,
				200.0,
				50.0,
				1.0
			}));
			xmlElement.AppendChild(this.creatXmlElement(xmlDocument, 1, new int[3], new double[]
			{
				300.0,
				300.0,
				600.0,
				400.0,
				50.0,
				1.0
			}));
			xmlElement.AppendChild(this.creatXmlElement(xmlDocument, 2, new int[3], new double[]
			{
				300.0,
				300.0,
				1000.0,
				1200.0,
				200.0,
				0.1
			}));
			xmlDocument.AppendChild(xmlElement);
			xmlDocument.Save(filename);
		}

		private XmlElement creatXmlElement(XmlDocument doc, int id, int[] selectValues, double[] inputValue)
		{
			string[] array = new string[]
			{
				"Hat",
				"Corner",
				"Slop"
			};
			string[] array2 = new string[]
			{
				"Radius",
				"Distance",
				"Width",
				"Deep",
				"Thickness",
				"Slop"
			};
			XmlElement xmlElement = doc.CreateElement("DirchKind");
			xmlElement.SetAttribute("ID", id.ToString());
			XmlElement xmlElement2 = doc.CreateElement("SelectIndex");
			for (int i = 0; i < array.Length; i++)
			{
				xmlElement2.SetAttribute(array[i], selectValues[i].ToString());
			}
			XmlElement xmlElement3 = doc.CreateElement("ParamValue");
			for (int j = 0; j < inputValue.Length; j++)
			{
				xmlElement3.SetAttribute(array2[j], inputValue[j].ToString());
			}
			xmlElement.AppendChild(xmlElement2);
			xmlElement.AppendChild(xmlElement3);
			return xmlElement;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public bool FreeControlTag
		{
			get
			{
				return this.m_bfreeControlTag;
			}
			set
			{
				this.m_bfreeControlTag = value;
			}
		}

		public double NowLengthLimitValue
		{
			get
			{
				return this.m_dNowLengthLimitValue;
			}
			private set
			{
				this.m_dNowLengthLimitValue = value;
			}
		}

		public double NowWidthLimitValue
		{
			get
			{
				return this.m_dNowWidthLimitValue;
			}
			private set
			{
				this.m_dNowWidthLimitValue = value;
			}
		}

		public double NowThicknessLimitValue
		{
			get
			{
				return this.m_dNowThicknessLimitValue;
			}
			private set
			{
				this.m_dNowThicknessLimitValue = value;
			}
		}

		public bool IfSendEscTag
		{
			get
			{
				return this.m_bIfSendEscTag;
			}
			set
			{
				this.m_bIfSendEscTag = value;
			}
		}

		public FormUse()
		{
			this.InitializeComponent();
		}

		public FormUse(IntPtr inputrOfForm, YRevitJigExtend jig, int MajorIndex)
		{
			this.InitializeComponent();
			this.m_mainWindowHandle = inputrOfForm;
			this.m_thisJig = jig;
			this.m_nMajorIndex = MajorIndex;
			this.Text = StaticRescource.StrDitchMajor[MajorIndex] + StaticRescource.strDitchDraw;
			List<string> list = new List<string>();
			switch (MajorIndex)
			{
			case 0:
				for (int i = 0; i < 4; i++)
				{
					list.Add(StaticRescource.StrArrayFlateNames[i]);
				}
				break;
			case 1:
			{
				list.Add(StaticRescource.StrArrayFlateNames[4]);
				int width = base.Width;
				int num = base.Height - base.ClientRectangle.Height;
				base.Height = num + this.groupBox_Line.Location.Y - base.Location.Y;
				base.Width = width;
				this.groupBox_Line.Visible = false;
				break;
			}
			case 2:
				for (int j = 0; j < 4; j++)
				{
					list.Add(StaticRescource.StrArrayFlateNames[j]);
				}
				break;
			}
			this.comboBox_DitchKind.Items.AddRange(list.ToArray());
			this.comboBox_CornerKind.SelectedIndex = 0;
			this.comboBox_DitchKind.SelectedIndex = 0;
			base.MouseLeave += this.FormUse_MouseLeave;
			foreach (object obj in base.Controls)
			{
				if (obj is ComboBox && (obj as ComboBox).DropDownStyle != ComboBoxStyle.DropDownList)
				{
					if (!(obj as ComboBox).Name.Equals("comboBox_Slope"))
					{
						(obj as ComboBox).KeyPress += this.eventHanlder_keyPressPulse;
					}
					else
					{
						(obj as ComboBox).KeyPress += this.eventHanlder_keyPressPulse;
					}
					(obj as ComboBox).Leave += this.eventHanlder_leave;
				}
			}
			this.comboBox_Radius.KeyPress += this.eventHanlder_keyPressPulse;
			this.comboBox_Radius.Leave += this.eventHanlder_leave;
			this.comboBox_Distance.KeyPress += this.eventHanlder_keyPressPulse;
			this.comboBox_Distance.Leave += this.eventHanlder_leave;
			base.FormClosed += this.FormUse_FormClosed;
			this.makeControlList();
			this.getXmlValue();
			this.conditionSet();
			this.comboxesValueReSet();
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.comboBox_Slope.Enabled = this.ifDrawSlopeTag;
			this.ReadFormData();
		}

		private void FormUse_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.setXmlValue();
			this.WriteFormData();
			if (this.IfSendEscTag)
			{
				this.sendEscapeMessage(false);
			}
		}

		private void conditionSet()
		{
			switch (this.comboBox_CornerKind.SelectedIndex)
			{
			case 0:
				this.comboBox_Radius.Enabled = false;
				this.comboBox_Distance.Enabled = false;
				break;
			case 1:
				this.comboBox_Distance.Enabled = true;
				this.comboBox_Radius.Enabled = false;
				break;
			case 2:
				this.comboBox_Distance.Enabled = true;
				this.comboBox_Radius.Enabled = false;
				break;
			case 3:
				this.comboBox_Radius.Enabled = true;
				this.comboBox_Distance.Enabled = false;
				break;
			}
			switch (this.m_nNowFamilyFileIndex)
			{
			case 0:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.圆孔箅子;
				return;
			case 1:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.铸铁箅子;
				return;
			case 2:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.实心混凝土盖板;
				return;
			case 3:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.钢筋混凝土盖板;
				return;
			case 4:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.实心混凝土盖板;
				return;
			case 5:
				this.pictureBox_DitchPictur.Image = HYCoordinate.Properties.Resources.实心混凝土盖板;
				return;
			default:
				return;
			}
		}

		public int GetFamilyFileIndex()
		{
			return this.m_nNowFamilyFileIndex;
		}

		public Dictionary<int, double> GetParameters()
		{
			Dictionary<int, double> result;
			try
			{
				Dictionary<int, double> dictionary = new Dictionary<int, double>();
				dictionary.Add(0, 0.0);
				dictionary.Add(1, 0.0);
				dictionary.Add(2, 0.0);
				dictionary.Add(3, 0.0);
				dictionary.Add(4, 0.0);
				dictionary.Add(5, 0.0);
				dictionary.Add(6, 0.0);
				dictionary[0] = this.MMStringToFeet(this.comboBox_Radius.Text);
				dictionary[1] = this.MMStringToFeet(this.comboBox_Distance.Text);
				dictionary[2] = this.MMStringToFeet(this.comboBox_FlateWidth.Text) + 2.0 * this.MMStringToFeet(this.comboBox_Thickness.Text);
				dictionary[3] = double.Parse(this.comboBox_Slope.Text);
				dictionary[4] = this.MMStringToFeet(this.comboBox_Deep.Text) + this.MMStringToFeet(this.comboBox_Thickness.Text);
				dictionary[5] = this.MMStringToFeet(this.comboBox_Thickness.Text);
				dictionary[6] = this.MMStringToFeet(this.comboBox_FlateWidth.Text) + 2.0 * Utiltiy.FeetMMChange((double)(StaticRescource.ValuesofFlateDistance[this.m_nNowFamilyFileIndex] - 5), false);
				result = dictionary;
			}
			catch (Exception)
			{
				throw new CornerMakerException(StaticRescource.StrError_WrongParameter);
			}
			return result;
		}

		public int GetCornerKind()
		{
			string text = this.comboBox_CornerKind.Text;
			if (text.Equals("单倒角"))
			{
				return 1;
			}
			if (text.Equals("倒角"))
			{
				return 2;
			}
			if (text.Equals("弧角"))
			{
				return 3;
			}
			return 0;
		}

		private void UpdataLimitValue()
		{
			this.NowLengthLimitValue = LimitValueFactory.GetLengthLimitValue(this.m_nNowFamilyFileIndex);
			this.NowWidthLimitValue = LimitValueFactory.GetWidthLimitValue(this.m_nNowFamilyFileIndex);
			this.NowThicknessLimitValue = LimitValueFactory.GetWidthLimitValue(this.m_nNowFamilyFileIndex);
		}

		private void ReadFormData()
		{
			string path = Path.GetTempPath() + "CableDitchFormPosition.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = 0;
			int y = 0;
			try
			{
				x = Convert.ToInt32(streamReader.ReadLine());
				y = Convert.ToInt32(streamReader.ReadLine());
			}
			catch
			{
				x = 0;
				y = 0;
			}
			streamReader.Close();
			fileStream.Close();
			Point location = new System.Drawing.Point(x, y);
			base.Location = location;
		}

		private void WriteFormData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "CableDitchFormPosition.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void makeNowFamilyFileIndex()
		{
			switch (this.m_nMajorIndex)
			{
			case 0:
				this.m_nNowFamilyFileIndex = this.comboBox_DitchKind.SelectedIndex;
				break;
			case 1:
				this.m_nNowFamilyFileIndex = 4;
				break;
			case 2:
				this.m_nNowFamilyFileIndex = this.comboBox_DitchKind.SelectedIndex + 5;
				break;
			}
			this.UpdataLimitValue();
		}

		private double MMStringToFeet(string input)
		{
			return Utiltiy.FeetMMChange(double.Parse(input), false);
		}

		private void FormUse_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.toolTip_useTip.SetToolTip(this.button_btWidthChange, "拾取宽度");
			this.toolTip_useTip.SetToolTip(this.button_DistanceValueChange, "拾取深度");
		}

		private void FormUse_MouseLeave(object sender, EventArgs e)
		{
			this.comboxesValueReSet();
			try
			{
				if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
				{
					int wMsg = 7;
					FormUse.SendMessage(this.m_mainWindowHandle, wMsg, IntPtr.Zero, IntPtr.Zero);
					this.m_thisJig.SetWidth(this.GetParameters()[2]);
				}
				else
				{
					base.Focus();
				}
			}
			catch (CornerMakerException ex)
			{
				if (!ex.Message.Equals(StaticRescource.StrError_WrongParameter))
				{
					throw ex;
				}
				YJKMessageBox.Show(StaticRescource.StrMessage_WrongParameter);
				base.Focus();
			}
		}

		private void comboBox_DitchKind_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.makeNowFamilyFileIndex();
			this.conditionSet();
		}

		private void eventHanlder_leave(object sender, EventArgs e)
		{
			if (!(sender is ComboBox))
			{
				return;
			}
			if ((sender as ComboBox).DropDownStyle == ComboBoxStyle.DropDownList)
			{
				return;
			}
			ComboBox comboBox = sender as ComboBox;
			string text = comboBox.Text;
			double num = 100.0;
			if (string.IsNullOrWhiteSpace(text) || string.IsNullOrEmpty(text) || !double.TryParse(text, out num))
			{
				YJKMessageBox.Show("请检查参数");
				comboBox.Text = num.ToString("F2");
				if (comboBox.Items.Count > 0)
				{
					comboBox.Text = comboBox.Items[0].ToString();
				}
				comboBox.Focus();
				return;
			}
			if (0.0 >= num)
			{
				YJKMessageBox.Show("请检查参数");
				comboBox.Focus();
				return;
			}
			if (comboBox.Name.Equals("comboBox_FlateWidth") && Geometry.LessThan(num, this.NowWidthLimitValue))
			{
				YJKMessageBox.Show("宽度过小请检查参数");
				num = this.NowWidthLimitValue;
			}
			else if (comboBox.Name.Equals("comboBox_Thickness") && Geometry.LessThan(num, this.NowThicknessLimitValue))
			{
				YJKMessageBox.Show("厚度过小请检查参数");
				num = this.NowThicknessLimitValue;
			}
			comboBox.Text = num.ToString("F2");
			FormUse.AddComboxItem(comboBox);
		}

		private static void AddComboxItem(ComboBox tempComboBox)
		{
			IEnumerator enumerator = tempComboBox.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Equals(tempComboBox.Text))
					{
						return;
					}
				}
			}
			tempComboBox.Items.Add(tempComboBox.Text);
		}

		private void eventHanlder_keyPressPulse(object sender, KeyPressEventArgs e)
		{
		}

		private void comboBox_CornerKind_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.conditionSet();
		}

		private void checkBox_IfSlope_CheckedChanged(object sender, EventArgs e)
		{
			this.ifDrawSlopeTag = this.checkBox_IfSlope.Checked;
			this.comboBox_Slope.Enabled = this.ifDrawSlopeTag;
		}

		private void button_DistanceValueChange_Click(object sender, EventArgs e)
		{
			this.FreeControlTag = true;
			this.comboBoxNeedValue = this.comboBox_Deep;
			this.sendEscapeMessage(true);
		}

		private void button_btWidthChange_Click(object sender, EventArgs e)
		{
			this.FreeControlTag = true;
			this.comboBoxNeedValue = this.comboBox_FlateWidth;
			this.sendEscapeMessage(true);
		}

		private void hyGroupBoxBlackSide1_Enter_1(object sender, EventArgs e)
		{
		}

		private void FormUse_Leave(object sender, EventArgs e)
		{
		}

		private void groupBox1_Enter(object sender, EventArgs e)
		{
		}

		private void pictureBox_DitchPictur_Click(object sender, EventArgs e)
		{
		}

		private void comboBox_Radius_Leave(object sender, EventArgs e)
		{
		}

		private bool ifDrawSlopeTag;

		private List<ComboBox> inputCombx = new List<ComboBox>();

		private List<Control> selectControl = new List<Control>();

		private ComboBox comboBoxNeedValue;

		private int m_nNowFamilyFileIndex = -1;

		private int m_nMajorIndex = -1;

		private IntPtr m_mainWindowHandle;

		private bool m_bfreeControlTag;

		private YRevitJigExtend m_thisJig;

		private double m_dNowLengthLimitValue;

		private double m_dNowWidthLimitValue;

		private double m_dNowThicknessLimitValue;

		private bool m_bIfSendEscTag;
	}
}
